package com.atguigu.gulimall.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsConfigurationSource;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
import org.springframework.web.server.ServerWebExchange;

@Configuration
public class GulimallCorsConfiguration {
//写跨域相关的代码
    @Bean   //注入到容器中
    public CorsWebFilter corsWebFilter() {
            //CorsConfigurationSource 他的实现是 UrlBasedCorsConfigurationSource
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration corsConfiguration = new CorsConfiguration();
           //1.配置跨域 容许那些头跨域
        corsConfiguration.addAllowedHeader("*");
            //2.容许那些请求方式跨域   get/post..
        corsConfiguration.addAllowedMethod("*");
            //3.容许那些请求来源跨域
        corsConfiguration.addAllowedOrigin("*");
             //4.是否容许携带 酷开
        corsConfiguration.setAllowCredentials(true);


        //代表任何路径 全部   corsConfiguration 配置的相关参数
        //registerCorsConfiguration  注册跨域的配置
        source.registerCorsConfiguration("/**",corsConfiguration);






        /*CorsConfigurationSource corsConfigurationSource = new CorsConfigurationSource() {
            @Override
            public CorsConfiguration getCorsConfiguration(ServerWebExchange exchange) {
                return null;
            }
        }
*/

        return new CorsWebFilter(source);
    }


}
